<?php
namespace app\stores\model\promotion;

use app\beautiful\model\goods\GoodsGroupModel;
use app\common\model\CommonModel;

use think\facade\Db;

class TeamActivityModel extends CommonModel{
    protected  $name = 'promotion_team_activity';
    protected  $pk   = 'team_activity_id';

    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function info($where,$field='*',$type=1){
        $info = Db::name('promotion_team_activity')->alias('ta')
            ->join('wyu_promotion_activity a','ta.activity_id=a.activity_id')
            ->join('wyu_beautiful_goods g','a.goods_id=g.goods_id')
            ->join('wyu_shop_detail sd','sd.shop_id=g.shop_id')
            ->join('wyu_shop s','s.shop_id=sd.shop_id')
            ->where(['ta.is_delete'=>0,'a.is_delete'=>0,'g.is_delete'=>0])
            ->when(isset($where['team_activity_id']) && !empty($where['team_activity_id']),function ($query) use ($where){
                $query->where(['ta.team_activity_id'=>$where['team_activity_id']]);
            })
            ->when(isset($where['type']),function ($query) use ($where){
                $query->where(['a.type'=>$where['type']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->field($field)
            ->find();
        if(!empty($info)){
            $info['start_time'] = date('Y-m-d',strtotime($info['start_time']));
            $info['end_time'] = date('Y-m-d',strtotime($info['end_time']));
            $info['sell_start_time'] = $info['sell_time_type']>0?date('Y-m-d',strtotime($info['sell_start_time'])):$info['sell_start_time'];
            $info['sell_end_time'] = $info['sell_time_type']>0?date('Y-m-d',strtotime($info['sell_end_time'])):$info['sell_end_time'];
            $info['use_limit_detail'] = $info['use_limit_detail']?json_decode($info['use_limit_detail'],true):$info['use_limit_detail'];
            if(isset($info['type']) && $info['type']==3){
                //套餐分组
                $goods_group = GoodsGroupModel::with(['groupDetail'=>function($query){
                        $query->where(['is_delete'=>0]);
                    }])->where([['goods_id','=',$info['goods_id']],['is_delete','=',0]])
                        ->field('goods_group_id,group_name,type,num,choose_num,is_choose_same')
                        ->select()->toArray() ?? [];
                foreach ($goods_group as $kk=>$vv){
                    if($vv['groupDetail']){
                        $groupDetail = $vv['groupDetail'];
                        foreach ($groupDetail as &$v){
                            $v['original_price'] = $v['original_price'] * $v['num'];
                        }
                        $goods_group[$kk]['groupDetail'] = $groupDetail;

                    }
                }
                $info['goods_group'] = $goods_group;
                $info['original_price'] = $info['total_value'];
            }
            $info['commission'] = $type==1?$info['team_commission']:$info['commission'];
        }
        return $info;
    }
    public static function ainfo($where,$field='*'){
        $info = Db::name('promotion_team_activity')->alias('ta')
            ->join('wyu_promotion_activity a','ta.activity_id=a.activity_id')
            ->where(['ta.is_delete'=>0,'a.is_delete'=>0])
            ->when(isset($where['team_activity_id']) && !empty($where['team_activity_id']),function ($query) use ($where){
                $query->where(['ta.team_activity_id'=>$where['team_activity_id']]);
            })->field($field)
            ->find();
        return $info;
    }
    public static function getId($where){
        $activity_ids = Db::name('promotion_team_activity')->where($where)->column('activity_id');

        return $activity_ids;
    }
    public static function list($where,$order,$field='*',$page=1,$limit=10){
        $query = Db::name('promotion_team_activity')->alias('ta')
            ->join('wyu_promotion_activity a','ta.activity_id=a.activity_id')
            ->join('wyu_beautiful_goods g','a.goods_id=g.goods_id')
            ->join('wyu_shop_detail sd','sd.shop_id=g.shop_id')
            ->join('wyu_shop s','s.shop_id=sd.shop_id')
            ->where(['ta.is_delete'=>0,'a.is_delete'=>0,'a.status'=>1,'g.is_delete'=>0])
            ->when(isset($where['team_id']) && !empty($where['team_id']),function ($query) use ($where){
                $query->where(['ta.team_id'=>$where['team_id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['ta.status'=>$where['status']]);
            })
            ->field($field);
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                if($v['type']==3) $v['original_price'] = $v['total_value'];
                unset($v['total_value']);
                $v['start_time'] = date('Y-m-d',strtotime($v['start_time']));
                $v['end_time'] = date('Y-m-d',strtotime($v['end_time']));
                $v['sell_start_time'] = $v['sell_time_type']>0?date('Y-m-d',strtotime($v['sell_start_time'])):$v['sell_start_time'];
                $v['sell_end_time'] = $v['sell_time_type']>0?date('Y-m-d',strtotime($v['sell_end_time'])):$v['sell_end_time'];
            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }

}
